package personal.xzhang.swing.jwizard;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JSeparator;

public abstract class JWizardDialog extends JDialog {

    private static final long serialVersionUID = 2L;
    
    protected WizardCards steps_;
    protected JPanel workAreaPanel_;
    protected JPanel footerPanel_;
    protected JButton nextButton_;
    protected JButton previousButton_;
    protected JButton cancelButton_;

    public JWizardDialog(WizardCards steps) {
        super();
        steps_ = steps;
        init();
    }

    protected void init() {
        getContentPane().setLayout(new BorderLayout());
        initWorkAreaPanel();
        getContentPane().add(workAreaPanel_, BorderLayout.NORTH);
        initFooterPanel();
        getContentPane().add(footerPanel_, BorderLayout.SOUTH);
    }

    private void initWorkAreaPanel() {
        workAreaPanel_ = new JPanel(new BorderLayout());
        steps_.addComponentToPane(workAreaPanel_);
    }

    private void initFooterPanel() {
        footerPanel_ = new JPanel(new BorderLayout());
        JPanel leftNavigatePanel = new JPanel(new FlowLayout());
        JPanel rightNavigatePanel = new JPanel(new FlowLayout());
        initNextButton();
        initPreviousButton();
        initCancelButton();
        footerPanel_.add(new JSeparator(), BorderLayout.NORTH);
        footerPanel_.add(cancelButton_, BorderLayout.WEST);
        rightNavigatePanel.add(previousButton_);
        rightNavigatePanel.add(nextButton_);
        leftNavigatePanel.add(cancelButton_);
        footerPanel_.add(leftNavigatePanel, BorderLayout.WEST);
        footerPanel_.add(rightNavigatePanel, BorderLayout.EAST);
    }

    private void initCancelButton() {
        cancelButton_ = new JButton("Cancel");
        cancelButton_.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cancel();
            }
        });
    }

    private void initPreviousButton() {
        previousButton_ = new JButton("Previous");
        previousButton_.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gotoPrevious();
            }
        });
        previousButton_.setEnabled(false);
    }

    private void initNextButton() {
        nextButton_ = new JButton("Next");
        nextButton_.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gotoNext();
            }
        });
        nextButton_.setEnabled(false);
    }

    protected abstract void gotoNext();

    protected abstract void gotoPrevious();

    protected abstract void cancel();
}